package com.fenxiangle.yueding.feature.focus.view;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.common.widget.popupwindow.PopupWindowList;
import com.fenxiangle.yueding.entity.bo.AliPayBo;
import com.fenxiangle.yueding.entity.bo.ExtensionBo;
import com.fenxiangle.yueding.feature.extension.contract.ExtensionContract;
import com.fenxiangle.yueding.feature.extension.login.DaggerExtensionComponent;
import com.fenxiangle.yueding.feature.extension.login.ExtensionPresenterModule;
import com.fenxiangle.yueding.feature.mine.view.ShareActivity;
import com.fenxiangle.yueding.framework.DataServer;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Home on 2018/9/1.
 */

public class ExtendCoreActivity extends BaseActivity implements ExtensionContract.View, Toolbar.OnMenuItemClickListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.extend_img)
    ImageView extendImg;
    @BindView(R.id.extend_name)
    TextView extendName;
    @BindView(R.id.extend_ID)
    TextView extendID;
    @BindView(R.id.layout_extend)
    LinearLayout layoutExtend;
    @BindView(R.id.layout_huiyuan)
    RelativeLayout layoutHuiyuan;
    @BindView(R.id.extend_recharge)
    TextView extendRecharge;

    UMImage image;//网络图片
    UMWeb umWeb;
    @Inject
    ExtensionContract.Presenter presenter;
    @BindView(R.id.extend_huiyuan)
    TextView extendHuiyuan;
    @BindView(R.id.commission)
    TextView commission;
    @BindView(R.id.tv_tui_guang)
    TextView tvTuiGuang;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_extend_recharge)
    TextView tvExtendRecharge;
    @BindView(R.id.tv_friend)
    RelativeLayout tvFriend;
    @BindView(R.id.tv_gudong)
    RelativeLayout tvGudong;
    ExtensionBo mData;
    @BindView(R.id.view_show_cz)
    LinearLayout mViewShowCz;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_extend_core;
    }

    @Override
    protected void initInjector() {
        DaggerExtensionComponent.builder().extensionPresenterModule(new ExtensionPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        super.initView();
        initToolBar(toolbar, "推广中心", true, true);


    }

    @Override
    protected void initData() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("jason.broadcast.action");
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                presenter.user_Extension_Center();
            }
        }, intentFilter);
        presenter.user_Extension_Center();
    }


    Intent intent = null;

    @OnClick({R.id.extend_ID, R.id.layout_extend, R.id.layout_huiyuan, R.id.extend_recharge, R.id.btn_to_level})
    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.extend_ID:
                break;
            case R.id.layout_extend:
                intent = new Intent(this, ExtendCommissionActivity.class);
                startActivityForResult(intent, 30000);
                break;
            case R.id.layout_huiyuan:
//                intent = new Intent(this, WithdrawDepositActivity.class);
//                intent.putExtra("pay", "1");
//                startActivity(intent);
                break;
            case R.id.extend_recharge:
                PopupWindowList popupWindowList = new PopupWindowList(this);
                popupWindowList.showMoreWindow(extendRecharge, "原始会员", "品牌达人", new PopupWindowList.PopupWindowListener() {
                    @Override
                    public void first() {
                        intent = new Intent(ExtendCoreActivity.this, WithdrawDepositActivity.class);
                        intent.putExtra("pay", "1");
                        intent.putExtra("type", "0");
                        startActivityForResult(intent, 30000);
                    }

                    @Override
                    public void second() {
                        intent = new Intent(ExtendCoreActivity.this, WithdrawDepositActivity.class);
                        intent.putExtra("pay", "1");
                        intent.putExtra("type", "1");
                        startActivityForResult(intent, 30000);
                    }
                });
                break;
            case R.id.btn_to_level:
                intent = new Intent(ExtendCoreActivity.this, DemandManageActivity.class);
                intent.putExtra("user_id", mData.getUserUid());
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 30000) {
            if (data != null && !TextUtils.isEmpty(data.getStringExtra("alibb"))) {
                presenter.user_Extension_Center();//提现完毕之后 刷新
            }
        }

        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }


    @Override
    public void ShowData(ExtensionBo s) {
        if (s != null) {
            mData = s;
            AM.image().bindToCircleObject(s.getUserHead(), extendImg);
            extendName.setText(s.getUserName());
            extendID.setText(TextUtils.isEmpty(s.getUserUid()) ? "" : "ID:" + s.getUserUid());
            tvTuiGuang.setText(TextUtils.isEmpty(s.getExtensionUserUid()) ? "" : "你是由（" + s.getExtensionUserUid() + "）推荐");
            tvNum.setText("" + s.getTeamNum());
            //0 无会员 1：原始会员 2：品牌达人
            if (s.getMembershipLevel().equals("0")) {
                extendRecharge.setVisibility(View.VISIBLE);
                tvExtendRecharge.setVisibility(View.VISIBLE);
            } else if (s.getMembershipLevel().equals("1")) {
                extendRecharge.setVisibility(View.GONE);
                layoutHuiyuan.setVisibility(View.VISIBLE);
                extendHuiyuan.setText("原始会员");
                Drawable drawable = getResources().getDrawable(R.drawable.icon_huiyuan);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getMinimumHeight());
                extendHuiyuan.setCompoundDrawables(drawable, null, null, null);
                tvExtendRecharge.setVisibility(View.GONE);
            } else if (s.getMembershipLevel().equals("2")) {
                tvExtendRecharge.setVisibility(View.GONE);
                extendRecharge.setVisibility(View.GONE);
                layoutHuiyuan.setVisibility(View.VISIBLE);
                extendHuiyuan.setText("品牌达人");
                Drawable drawable = getResources().getDrawable(R.mipmap.icon_pingpan);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getMinimumHeight());
                extendHuiyuan.setCompoundDrawables(drawable, null, null, null);
            }
            commission.setText("￥" + s.getCommission());
            tvTuiGuang.setVisibility(TextUtils.isEmpty(s.getExtensionUserUid()) ? View.GONE : View.VISIBLE);

            image = new UMImage(this, s.getUserHead());
            umWeb = new UMWeb("http://yuedingapp.com/meeting-api/share/share.html?id=" + s.getUserUid());
            umWeb.setTitle(s.getUserName());//标题
            umWeb.setThumb(image);  //缩略图
            umWeb.setDescription("悄悄告诉你：不太守信的人都不敢玩约定。");//描述




            if (AM.user().isLogin()) {
                if (AM.user().getLoginResultCache().getUserPhone().equals(DataServer.LOCAL_USER)) {
                    extendRecharge.setVisibility(View.GONE);
                }
            }
        }
    }

    @Override
    public void showLoginSuccess() {

    }

    @Override
    public void showAliPayBo(AliPayBo aliPayBo) {

    }

    @Override
    public void showWeiChatData(AliPayBo aliPayBo) {

    }

    @Override
    public void showError(String error) {

    }

    @Override
    public void showPwd(String pwd) {

    }

    @Override
    public void user_payment_set() {

    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {

        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {

        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {

        }
    };


    @OnClick({R.id.tv_friend, R.id.tv_gudong})
    public void onVisewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_friend:
                new ShareAction(this).withMedia(umWeb)
                        .setDisplayList(SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA)
                        .setCallback(umShareListener).open();
                break;
            case R.id.tv_gudong:
                startActivity(new Intent(this, ShareActivity.class));
                break;
        }
    }


    @Override
    public boolean onMenuItemClick(MenuItem item) {
        int menuItemId = item.getItemId();
        if (menuItemId == R.id.action_wallet_detail) {
            Intent intent = new Intent(this, WalletDetailActivity.class);
            startActivity(intent);
        }
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
